"use strict";
function SGOUI_checkbox(parentNode, name, txt, state)
{    
    var label;
    label = document.createElement('label')
    label.htmlFor = name;
    label.appendChild(document.createTextNode(txt));
    parentNode.appendChild(label);

    this.checkbox = document.createElement("input");
    this.checkbox.type = "checkbox";
    this.checkbox.name = name;
    parentNode.appendChild(this.checkbox);
    parentNode.appendChild(document.createElement("br"));      
    this.setState(state);
}

SGOUI_checkbox.prototype.setState=function(state)
{
    this.checkbox.checked=state;
}

SGOUI_checkbox.prototype.getState=function()
{
    return this.checkbox.checked;
}

/*** slide but ***/
function SGOUI_number(parentNode, name, txt, value)
{    
    var label;
    label = document.createElement('label')
    label.htmlFor = name;
    label.appendChild(document.createTextNode(txt));
    parentNode.appendChild(label);

    this.spin = document.createElement("input");
    this.spin.type = "number";
    this.spin.name = name;
    this.spin.min=1;    
    this.spin.max=20;      
    this.spin.value=Math.abs(value/1000);
    parentNode.appendChild(this.spin);
    //parentNode.appendChild(document.createElement("br"));      
    this.setValue(value);    
    
}

SGOUI_number.prototype.getValue=function()
{
    return this.spin.value*1000;
}

SGOUI_number.prototype.setValue=function(value)
{
   this.spin.value=Math.abs(value/1000);
}

function SGoUI_popup()
{
    this.m_container=null;
    this.m_textarea=null;
    this.cobut=null;
    this.edbut=null;
    this.autoplay = null;
    this.sgoObj=null;
}

/*** dialog div ***/
SGoUI_popup.prototype.create=function()
{
    var form, fset, legend, but, dv;
    
    self=this;
    
    this.m_container=document.createElement('div');
    this.m_container.id="sgo_dialog";
    
    form=document.createElement('form');
    fset=document.createElement('fieldset');    
    fset.id="sgo_dialog_config";
    legend=document.createElement('legend');
    legend.innerHTML="Config";
    fset.appendChild(legend);
    
    form.appendChild(fset);
    /* coordinates */
    this.cobut=new SGOUI_checkbox(fset,"sgo_dialog_coordinates","Coordinates",false);
    /* edit mode */
    this.edbut=new SGOUI_checkbox(fset, "sgo_dialog_editmode","Edit mode",false);
    /* autoplay */    
    this.autoplay=new SGOUI_number(fset, "sgo_autoplay_timeout", "Autoplay", 2000);    

    fset=document.createElement('fieldset');
    legend=document.createElement('legend');
    legend.innerHTML="SGF";
    
    fset.appendChild(legend);
    this.m_textarea=document.createElement("textarea");     
    fset.appendChild(this.m_textarea);
    form.appendChild(fset);
    this.m_container.appendChild(form);
    
    but=document.createElement('button');     
    but.innerHTML="Close";
    but.addEventListener("click",function(){self.hide()});
    dv=document.createElement('div');
    dv.innerHTML="<a href='http://www.govortex.ro/sgo'>SGoban</a> v1.2.0 &copy; Daniel Cioata ";
    dv.style.fontFamily="Arial,Helvetica";
    dv.style.fontSize="80%";
    dv.style.textAlign="right";
    this.m_container.appendChild(dv);
    dv=document.createElement('div');
    dv.style.textAlign="center";
    dv.style.marginTop="3px";
    dv.appendChild(but);
    this.m_container.appendChild(dv);                                                   
    document.body.appendChild(this.m_container);                                
}

SGoUI_popup.prototype.show=function(obj)
{    
    if(this.m_container==null)
    {            
        this.create();        
    }
    this.m_container.style.display='';       
    this.sgoObj=obj;
    this.m_textarea.value=this.sgoObj.m_rawsgf;
    
    this.cobut.setState(this.sgoObj.m_config.coordinates);
    this.edbut.setState(this.sgoObj.m_config.editmode);
    this.autoplay.setValue(this.sgoObj.m_config.timeout);
    
}

SGoUI_popup.prototype.hide=function()
{        
    this.m_container.style.display='none';
    this.setConfig();
       
}

SGoUI_popup.prototype.setConfig=function()
{
    if(this.sgoObj.m_config.coordinates !=  this.cobut.getState())
    {
        this.sgoObj.m_config.coordinates =  this.cobut.getState();        
        this.sgoObj.UIupdate();        

    }
    if(this.sgoObj.m_config.editmode !=  this.edbut.getState())
    {   
        this.sgoObj.m_config.editmode =  this.edbut.getState();     
        this.sgoObj.setEditable();        
    }
    if(this.sgoObj.m_config.timeout!=this.autoplay.getValue())
    {
        this.sgoObj.m_config.timeout=this.autoplay.getValue();        
    }
}
